﻿using System.Windows;
using System.Reflection;
using System.Windows.Controls;

using Builderator.Core.Components;

namespace Builderator.Firefly.Controls
{
    /// <summary>
    /// Interaction logic for ServerListControl.xaml
    /// </summary>
    public partial class ServerListControl : UserControl, IProperty
    {
        public static DependencyProperty ListProperty = DependencyProperty.Register("List", typeof(ServerList), typeof(ListControl));

        public new event PropertyChanged OnPropertyChanged;

        public ServerList List
        {
            get
            {
                return (ServerList)this.GetValue(ListProperty);
            }
            set
            {
                this.SetValue(ListProperty, value);

                this.itemListBox.ItemsSource = value;

                if (this.OnPropertyChanged != null)
                    this.OnPropertyChanged(this.Property, value);
            }
        }

        public PropertyInfo Property
        {
            get;
            set;
        }

        private ServerListControl()
        {
            InitializeComponent();
        }

        public ServerListControl(PropertyInfo property)
            : this()
        {
            this.Property = property;
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            DomainBrowserWindow domainBrowser = new DomainBrowserWindow();

            if (domainBrowser.ShowDialog() == true)
            {
                if (this.List.Contains(domainBrowser.Selected))
                    MessageBox.Show("Server already exists in domain", "Warning", MessageBoxButton.OK);

                this.List.Add(domainBrowser.Selected);
            }
        }

        private void removeButton_Click(object sender, RoutedEventArgs e)
        {
            this.List.Remove((string)this.itemListBox.SelectedItem);
        }
    }
}
